﻿using System;
using System.ComponentModel;
using System.Web.UI;

namespace MyLib.Web.UI {
	/// <summary>
	/// 
	/// </summary>
	[DefaultProperty("Digit")]
	[ToolboxData("<{0}:MyPostalCodeValidator runat=\"server\"></{0}:MyPostalCodeValidator>")]
	public sealed class MyPostalCodeValidator : MyFormatBaseValidator {
		/// <summary>
		/// 
		/// </summary>
		private const int DefaultDigit = 6;

		/// <summary>
		/// 
		/// </summary>
		[Category("Appearance")]
		[Bindable(false)]
		[Browsable(true)]
		[DefaultValue(DefaultDigit)]
		public int Digit {
			get {
				var v = ViewState["Digit"];
				if (v == null) {
					return DefaultDigit;
				} else {
					return (int)v;
				}
			}
			set {
				if (value < 1) {
					throw new ArgumentOutOfRangeException("Value of Digit cannot less than 1, bus was " + value);
				}

				ViewState["Digit"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		protected override string GetExpression() {
			return string.Format(@"[1-9]\d{{{0}}}", Digit - 1);
		}
	} // end of MyEmailValidator.
}
